package com.shemy.fileservice.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;
import java.io.File;

/**
 * @Description SecurityConfig
 * @Author Dzsom
 * @Date 2018-10-24 7:34
 * @Encoding UTF-8
 * @Version 1.0
 **/
@Configuration
@EnableWebMvc
public class SecurityConfig extends WebMvcConfigurerAdapter {
    @Resource
    private FileConfig fileConfig;

    @Bean
    AuthorityIPInterceptor ipInterceptor(){
        return new AuthorityIPInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(ipInterceptor()).addPathPatterns("/**");
        super.addInterceptors(registry);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/show/**").addResourceLocations("file:"+fileConfig.getHardDisk()+ File.separator);
        super.addResourceHandlers(registry);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**");
        super.addCorsMappings(registry);
    }
}
